package com.qbw.core.okhttp;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
import com.lib.util.DeviceUtil;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.exception.HttpException;
import com.lzy.okgo.request.base.Request;
import com.qbw.app.BuildConfig;
import com.qbw.app.manager.UserManager;
import com.qbw.app.model.User;
import com.qbw.app.module.common.util.CountryUtil;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import okhttp3.Response;
import okhttp3.ResponseBody;

public abstract class JsonCallback<T> extends AbsCallback<T> {
    private Type type;
    private Class<T> clazz;

    public JsonCallback() {
    }

    public JsonCallback(Type type) {
        this.type = type;
    }

    public JsonCallback(Class<T> clazz) {
        this.clazz = clazz;
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onStart(Request<T, ? extends Request> request) {
        super.onStart(request);
        User user = UserManager.getInstance().getUser();
        String token = user != null ? user.getToken() : null;
        if (!TextUtils.isEmpty(token)) {
            request.headers("Authorization", token);
        }
        request.headers("languageId", CountryUtil.getLanguageId() + "");
        request.headers("version", "1");
        request.headers("platform", "android");
        request.headers("device", android.os.Build.MODEL);
        request.headers("channel", BuildConfig.FLAVOR);
//        request.headers("imsi", DeviceUtil.getIMSI());
//        request.headers("deviceId", DeviceUtil.getIMEI(context));

    }

    @Override
    public void onError(com.lzy.okgo.model.Response<T> response) {
        super.onError(response);
        Throwable exception = response.getException();
        if (exception instanceof UnknownHostException || exception instanceof ConnectException) {
        } else if (exception instanceof SocketTimeoutException) {
        } else if (exception instanceof HttpException) {
        }else{
            if(response.getRawResponse()!=null){

            }
        }
    }



    /**
     * 该方法是子线程处理，不能做ui相关的工作
     * 主要作用是解析网络返回的 response 对象,生产onSuccess回调中需要的数据对象
     * 这里的解析工作不同的业务逻辑基本都不一样,所以需要自己实现,以下给出的时模板代码,实际使用根据需要修改
     */
    @Override
    public T convertResponse(Response response) throws Throwable {
        ResponseBody body = response.body();
        if (body == null) return null;
        T data = null;

        JsonReader jsonReader = new JsonReader(body.charStream());
        Gson gson = new GsonBuilder()
                .create();
        if (response.code() < 300 && response.code() >= 200) {
            if (type != null) {
                data = gson.fromJson(jsonReader, type);
            } else if (clazz != null) {
                data = gson.fromJson(jsonReader, clazz);
            } else {
                Type genType = getClass().getGenericSuperclass();
                Type type = ((ParameterizedType) genType).getActualTypeArguments()[0];
                data = gson.fromJson(jsonReader, type);
            }
        } else {
            throw new IllegalStateException();
        }

        return data;
    }

}
